// npm install --save-dev gulp del gulp-babel gulp-sftp minimist

// 清空dist
// gulp clean

// babel编译
// gulp build

// 编译并部署到test服务器
// gulp publish --env test

// 编译并部署到production服务器
// gulp publish --env production

const gulp = require('gulp')
const minimist = require('minimist')
const del = require('del')
const babel = require('gulp-babel')
const sftp = require('gulp-sftp')

const gulpConf = require('./gulpconf.json')

gulp.task('env', () => {
  const env = process.env.NODE_ENV = minimist(process.argv).env
  console.log('## 当前环境：', env || '无')
})

gulp.task('clean', () => {
  console.log('## 清空工作区...')
  return del(['dist'])
})

gulp.task('build', ['clean'], () => {
  console.log('## babel编译中...')
  return gulp.src('src/**')
    .pipe(babel())
    .pipe(gulp.dest('dist'))
})

gulp.task('deploy', ['build'], () => {
  const env = process.env.NODE_ENV
  if (!env) throw new Error('Missing NODE_ENV')

  const ftpConf = gulpConf.ftp[env]
  console.log('## 正在部署到远端服务器（' + ftpConf.host + ':' + ftpConf.remotePath + '）...')
  return gulp.src('dist/**')
    .pipe(sftp(ftpConf))
})

gulp.task('publish', ['env', 'deploy'], () => {
  console.log('## 部署完成！')
})
